/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.config;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.SAXException;

/**
 *
 * @author QuocThanh
 */
public class ConnectionConfig {
   

    //<editor-fold defaultstate="collapse" desc="1.Các thuộc tính">
    private String typeConnection;
    private String typeData;
   
    private String host;
    private int port;
    private String database;
    private String user;
    private String password;
    private String encoding;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapse" desc="2.Các phương thức khởi tạo">
    
    public ConnectionConfig(){
        this.typeConnection="jdbc";
        this.typeData="mysql";
        this.host="localhost";
        this.port=3306;
        this.database="";
        this.user="root";
        this.password="";
        this.encoding="utf8";
    }
    public ConnectionConfig(String typeConnection, String typeData,String host,int port, String database, String user, String password, String encoding) {
        this.typeConnection=typeConnection;
        this.typeData=typeData;
        this.host = host;
        this.port=port;
        this.database = database;
        this.user = user;
        this.password = password;
        this.encoding = encoding;
    }

     public ConnectionConfig(ConnectionConfig config){
        this.typeConnection=config.typeConnection;
        this.typeData=config.typeData;
        this.host=config.host;
        this.port=config.port;
        this.database=config.database;
        this.user=config.user;
        this.password=config.password;
        this.encoding=config.encoding;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapse" desc="3.Getter/Setter">
    
    public String getTypeConnection() {
        return typeConnection;
    }

    public void setTypeConnection(String typeConnection) {
        this.typeConnection = typeConnection;
    } 
     
    public String getTypeData() {
        return typeData;
    }

    public void setTypeData(String typeData) {
        this.typeData = typeData;
    }
    
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
       
    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }
    
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
   
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

     public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapse" desc="4.Đọc ghi file">
    
    public boolean Read(){
        try {
            
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=factory.newDocumentBuilder();
            Document doc=builder.parse(getClass().getResourceAsStream("Config.xml"));
            Element root=doc.getDocumentElement();
            //lay ve danh sach cac node con cua 1 node
            NodeList list=root.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node node=list.item(i);
                if(node instanceof Element){
                    Element element=(Element)node;
                    if(element.getTagName().equals("typeConnection")){
                        this.typeConnection=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("typeData")){
                        this.typeData=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("host")){
                        this.host=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("port")){
                        this.port=Integer.parseInt(element.getFirstChild().getNodeValue());
                    }else if(element.getTagName().equals("database")){
                        this.database=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("user")){
                        this.user=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("pass")){
                        this.password=element.getFirstChild().getNodeValue();
                    }else if(element.getTagName().equals("encoding")){
                        this.encoding=element.getFirstChild().getNodeValue();
                    }
                }
            }
            //doc file config thanh cong
            return true;
        } catch (SAXException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        //doc file config that bai
        return false;
    }
    
    public boolean Save(){
        try {
            
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=factory.newDocumentBuilder();
            Document doc=builder.parse(getClass().getResourceAsStream("Config.xml"));
            Element root=doc.getDocumentElement();
            //lay ve danh sach cac node con cua 1 node
            NodeList list=root.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node node=list.item(i);
                if(node instanceof Element){
                    Element element=(Element)node;
                    if(element.getTagName().equals("typeConnection")){
                        element.getFirstChild().setNodeValue(this.typeConnection);
                    }else if(element.getTagName().equals("typeData")){
                        element.getFirstChild().setNodeValue(this.typeData);
                    }else if(element.getTagName().equals("host")){
                        element.getFirstChild().setNodeValue(this.host);
                    }else if(element.getTagName().equals("port")){
                        element.getFirstChild().setNodeValue(String.valueOf(this.port));
                    }else if(element.getTagName().equals("database")){
                        element.getFirstChild().setNodeValue(this.database);
                    }else if(element.getTagName().equals("user")){
                        element.getFirstChild().setNodeValue(this.user);
                    }else if(element.getTagName().equals("pass")){
                        element.getFirstChild().setNodeValue(this.password);
                    }else if(element.getTagName().equals("encoding")){
                        element.getFirstChild().setNodeValue(this.encoding);
                    }
                    
                }
            }
            FileOutputStream output = new FileOutputStream(ClassLoader.getSystemResource("Config.xml").getFile());
            XMLSerializer ser = new XMLSerializer(output, new OutputFormat(doc, "UTF-8", true));
            ser.serialize(doc);
            output.close();
            //save file config thanh cong
            return true;
        } catch (SAXException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ConnectionConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        //save file config that bai
        return false;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapse" desc="5.Các phương thức override">
     @Override
    public String toString() {
        return "ConnectionConfig{" + "typeConnection=" + typeConnection + ", typeData=" + typeData + ", host=" + host + ", port=" + port + ", database=" + database + ", user=" + user + ", password=" + password + ", encoding=" + encoding + '}';
    }   
    
    //</editor-fold>

   
        

       
}
